package HUZ;

public class demo6 {

    public static void main2(String[] args) {
        Thread thread=new Thread(()->{
            System.out.println("start标志:"+Thread.currentThread().isInterrupted());
            Thread.currentThread().interrupt();
            System.out.println("end标志:"+Thread.currentThread().isInterrupted());
            System.out.println("end1标志:"+Thread.currentThread().interrupted());

            System.out.println("end2标志:"+Thread.currentThread().isInterrupted());
            System.out.println("end3标志:"+Thread.currentThread().interrupted());
            System.out.println("end4标志:"+Thread.currentThread().interrupted());



        });
        thread.start();
    }


    public static void main(String[] args) throws InterruptedException {
        System.out.println("主线程启动---");
        Thread t = new Thread(() -> {
            System.out.println("======当前标志位======:"+Thread.currentThread().isInterrupted());

            //其中这里的Thread.currentThread()是用于获取当前线程引用的方法
            //使用Interrupted来判断线程的中断标识信息(会清楚中断信息,所以当收到interrupt发送的中断信息后还会jx)
            while (!Thread.currentThread().interrupted()) {
                System.out.println("start当前标志位:"+Thread.currentThread().isInterrupted());
                System.out.println("线程运行中....");
                try {
                    //给线程t发送中断信息

                    Thread.sleep(4000);
                } catch (InterruptedException e) {

                    //interrupt产生行为并且当捕获到异常时进行代码处理的代码区域
                    //也就是你自己决定是退出还是继续的区域
                    System.out.println("捕获到异常，自己处理异常的代码区");
                    System.out.println("新线程即将退出!");
                }
                System.out.println("end当前标志位:"+Thread.currentThread().isInterrupted());

            }
            System.out.println("新线程已经退出!");
        });
        t.start();
        Thread.sleep(1000);

        t.join();
        Thread.sleep(3000);
        System.out.println("主线程退出!---");
    }
}
